﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Business.HYmodel;
using Business.ProductsTypeManagement.Dto;
using Hangfire.Dashboard;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using XCZ;

namespace Business.ProductsTypeManagement
{
    public class ProductsTypeAppService: BusinessWorkFlowAppService, IProductsTypeAppService
    {
        private IRepository<ProductsType, Guid> _repository;
        public ProductsTypeAppService(
            IRepository<ProductsType, Guid> repository
            )
        {
            _repository = repository;
        }
        /// <summary>
        /// 新增产品类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Dto.MenuItem> AddProductType(Dto.MenuItem input)
        {
            ProductsType type=ObjectMapper.Map<Dto.MenuItem, ProductsType>(input);
            return ObjectMapper.Map<ProductsType, Dto.MenuItem>(await _repository.InsertAsync(type));
        }
        /// <summary>
        /// 获取菜单项列表
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<TreeDto>> GetMenuItemList(int pId)
        {
            // 从仓储获取菜单项列表
            var list = await _repository.GetListAsync();

            // 构建菜单项
            var menuItems = BuildMenuItems(list, pId);

            // 返回分页结果dto包含菜单项
            return new PagedResultDto<TreeDto>
            {
                Items = menuItems
            };
        }
        /// <summary>
        /// 递归构建菜单项
        /// </summary>
        /// <param name="list"></param>
        /// <param name="pId"></param>
        /// <returns></returns>
        private List<TreeDto> BuildMenuItems(List<ProductsType> list, int pId)
        {
            var menuItems = list
                .Where(x => x.PId == pId)
                .Select(c => new TreeDto
                {
                    value = c.ProductsTypeId,
                    label = c.ProductsTypeName,
                    children = BuildMenuItems(list, c.ProductsTypeId).Count()>0?null: BuildMenuItems(list, c.ProductsTypeId), // 递归调用自身来获取子菜单项
                })
                .ToList();

            return menuItems;
        }
        /// <summary>
        /// 删除菜单项
        /// </summary>
        /// <param name="productsTypeId">要删除的菜单项的 ID</param>
        /// <returns>操作是否成功的布尔值</returns> 
        public async Task<bool> DeleteProductType(int productsTypeId)
        {
            // 检查要删除的菜单项是否存在
            var type = await _repository.GetAsync(x=>x.ProductsTypeId==productsTypeId);
            if (type == null)
            {
                // 如果菜单项不存在，返回 false 表示操作失败
                return false;
            }

            // 删除菜单项
            await _repository.DeleteAsync(type);

            // 返回 true 表示操作成功
            return true;
        }
        /// <summary>
        /// 获取菜单项详情
        /// </summary>
        /// <param name="productsTypeId">菜单项 ID</param>
        /// <returns></returns>
        public async Task<Dto.MenuItem> GetProductType(int productsTypeId)
        {
            // 从仓储获取菜单项
            var type = await _repository.GetAsync(x => x.ProductsTypeId == productsTypeId);

            // 映射菜单项到输出dto
            return ObjectMapper.Map<ProductsType, Dto.MenuItem>(type);
        }
        /// <summary>
        /// 修改菜单项111
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Dto.MenuItem> UpdateProductType(Dto.MenuItem input)
        {
            // 从仓储获取要修改的菜单项
            var type = await _repository.GetAsync(x => x.ProductsTypeId == input.ProductsTypeId);

            // 映射输入参数到菜单项
            ObjectMapper.Map(input, type);

            // 更新菜单项
            return ObjectMapper.Map<ProductsType, Dto.MenuItem>(await _repository.UpdateAsync(type));
        }
    }
}
